package com.sxfq.loansupermarket.backsystem.modular.report.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IOutpatientManagementService;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IPayServiceService;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.MarketReportTwoInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.entity.SearchMarketReportTwoInfo;
import com.sxfq.loansupermarket.backsystem.modular.report.service.IMarketReportTwoService;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.DepartmentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author py
 * @Date 2018年4月19日23:15:54 
 */
@Controller
@RequestMapping("/marketReportTwo")
public class marketReportTwoController extends BaseController {
    @Autowired
    IPayServiceService payServiceService;

    @Autowired
    IMarketReportTwoService service;

    @Autowired
    private SqlMapper sqlMapper;

    @Autowired
    IOutpatientManagementService outpatientManagementService;

    private String PREFIX = "/report/wholeHospital/";

    /**
     * 跳转到市场科室报表页面
     */
    @RequestMapping("")
    public String to_marketReportTwo(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        if (ShiroKit.getSessionAttr("dimensionList") == null) {
            String dimensionsql = "select id, dimension_china_name as dimensionChinaName," +
                    " dimension_english_name as dimensionEnglishName from market_dimension_info order by num;";
            List<Map<String, Object>> dimensionList=sqlMapper.selectList(dimensionsql);
            ShiroKit.setSessionAttr("dimensionList", dimensionList);
        }
        if (ShiroKit.getUser().getDepartmentList() == null) {
            String getListSql = "select a.department as department from " + ShiroKit.getUser().getPay_service_grade_info() + " a where a.is_upper=1 group by a.department order by a.id;";
            List<DepartmentInfo> departmentList=sqlMapper.selectList(getListSql,DepartmentInfo.class);
            ShiroKit.getUser().setDepartmentList(departmentList);
        }
        model.addAttribute("departmentList",ShiroKit.getUser().getDepartmentList());
        model.addAttribute("dimensionList", ShiroKit.getSessionAttr("dimensionList"));
        List<Map<String, Object>> outpatientManagementList = outpatientManagementService.lists(ShiroKit.getUser().getAccount());
        model.addAttribute("outpatientManagementList", outpatientManagementList);
        model.addAttribute("arrivalStoreValue", ShiroKit.getUser().getOutpatientName());
        model.addAttribute("sttleAccountsTime", ShiroKit.getUser().getSttleAccountsTime());
        return PREFIX + "marketReportTwo.html";
    }

    /**
     * 科室报表列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(SearchMarketReportTwoInfo searchInfo, HttpServletRequest request) {
        Page<MarketReportTwoInfo> page = new PageFactory<MarketReportTwoInfo>().defaultPage();
        try {
            List<MarketReportTwoInfo> list = new ArrayList<MarketReportTwoInfo>();
            if ("treatmentUnit".equals(searchInfo.getDimensionSelection())) {
                list = service.getTreatmentUnitListTwo(searchInfo);
            }else {
                list = service.getTreatmentUnitListsTwo(searchInfo);
            }
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }
}